package com.lv.common.widget.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.lv.common.R
import com.lv.common.ktx.LoadSrc

/**
 * 作者： 志威  zhiwei.org
 * 主页： Github: https://github.com/zhiwei1990
 * 日期： 2020年10月08日 14:24
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/  -- 志威 zhiwei.org
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 自定义的设置item的组合控件
 */
class EmptyView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : ConstraintLayout(context, attrs, defStyleAttr) {

    private var mIvEmpty: ImageView? = null
    private var mTvEmpty: TextView? = null
    private var mTvGoLogin: TextView? = null
    private var mState: State = State.CANTER;

    private var goLoginListener: View.OnClickListener? = null

    init {
        var inflate: View? = null
        if (mState == State.CANTER) {
            inflate = inflate(context, R.layout.empty_center_view, this)
        } else {
            inflate = inflate(context, R.layout.empty_view, this)
        }
        mIvEmpty = inflate.findViewById<ImageView>(R.id.iv_img)
        mTvEmpty = inflate.findViewById<TextView>(R.id.tv_empty)
        mTvGoLogin = inflate.findViewById<TextView>(R.id.tv_gologin)

        mTvGoLogin?.setOnClickListener {
            goLoginListener?.onClick(this)
        }


    }

    //region 设置资源

    /**
     * 设置title
     */
    fun setEmptyState(state: State) {
        mState = state
        invalidate()
    }

    /**
     * 设置title
     */
    fun setEmptyHint(hint: String) {
        mTvEmpty?.text = hint
    }

    /**
     * 设置标题显示
     */
    fun setEmtptyHintVis(isShow: Boolean) {
        if (isShow) {
            mTvEmpty?.visibility = VISIBLE
        } else {
            mTvEmpty?.visibility = GONE
        }
    }

    /**
     * 设置Login标题显示
     */
    fun setLoginVis(isShow: Boolean) {
        if (isShow) {
            mTvGoLogin?.visibility = VISIBLE
        } else {
            mTvGoLogin?.visibility = GONE
        }
    }

    /**
     * 设置Login标题显示
     */
    fun setLoginText(text: String) {
        mTvGoLogin?.text = text
    }

    /**
     * 设置icon图标
     */
    fun setEmptyRes(iconRes: Int) {
        mIvEmpty?.LoadSrc(iconRes)
    }


    enum class State {
        TOP, CANTER
    }
}
